package empleados.clases.administracion.componentes;

import empleados.ventana.componentes.interfaz.TablaEditableModelo;
import empleados.clases.entidades.BaseDatos;

import java.sql.ResultSet;

import java.util.TreeSet;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JTable;

import java.awt.event.ActionEvent;

public class AdministracionPais{

    private BaseDatos bd;

    private int id;

    public AdministracionPais(int id){
        this.id = id;
        bd = new BaseDatos("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/terafit", "root", "");
    }

    public int borrarPais(){
        int filasAfectadas = 0;
        try{
            filasAfectadas = bd.ejecutarQuery("DELETE FROM pais WHERE id = " + this.id + ";");
            System.out.println("DELETE FROM pais WHERE id = " + this.id + ";");
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("No existe esa localidad.");
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            return filasAfectadas;
        }
    }

    public void modificarPais(){
    }

    public void setId(int id){
        this.id = id;
    }

    public Action borrarPaisAction(){
        Action borrarPais = new AbstractAction(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource() instanceof JTable){
                    JTable table = (JTable)e.getSource();
                    int id;
                    int modelRow = Integer.valueOf(e.getActionCommand());
                    id = (Integer)((TablaEditableModelo) table.getModel()).getValueAt(modelRow, 0);
                    setId(id);
                    borrarPais();
                    ((TablaEditableModelo) table.getModel()).removeRow(modelRow);
                }else{
                    System.out.println("Error en seleccion de tabla.");
                }
            }
        };
        return borrarPais;
    }

    public Action modificarPaisAction(){
        Action modificarPais = new AbstractAction(){
            public void actionPerformed(ActionEvent e){
                JTable table = (JTable)e.getSource();
                int modelRow = Integer.valueOf( e.getActionCommand() );
                int rows = ((TablaEditableModelo) table.getModel()).getRowCount();
                for(int i=0; i<rows;i++){
                    if (modelRow != i ){
                        ((TablaEditableModelo) table.getModel()).setCeldaEditable(i,1,false);
                        //((TablaEditableModelo) table.getModel()).setValueAt();
                    }else{
                        ((TablaEditableModelo) table.getModel()).setCeldaEditable(i,1,true);
                        System.out.println(((TablaEditableModelo) table.getModel()).getValueAt(i, 2));
                    }
                }
                System.out.println(modelRow);
                table.repaint();
                
            }
        };
        return modificarPais;
    }

}